import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/one/loginView.vue')
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/one/registerView.vue')
    },
    {
        path: '/',
        name: 'Layout',
        component: () => import('@/views/one/layoutView.vue'),
        redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import('@/views/two/HomeView.vue')
            }, {
                path: 'product/list',
                name: 'productList',
                component: () => import('@/views/two/product/listView.vue')
            }, {
                path: 'product/classify',
                name: 'productClassify',
                component: () => import('@/views/two/product/CategoryView.vue')
            }
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    }
    const user = localStorage.getItem("user");
    if (!user && to.path !== '/login') {
        return next('/login');
    }
    next();
})

export default router
